package sanglh.edu.hsphysics.utils;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;

public class FragmentUtil {
	public static void replace(FragmentManager fragmentManager,
			int containerId, Fragment fragment, boolean addToBackStack) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (addToBackStack) {
			transaction.addToBackStack(null);
		}
		if (null == fragment) {
			return;
		}
		transaction.setCustomAnimations(android.R.anim.fade_in,
				android.R.anim.fade_out);
		transaction.replace(containerId, fragment, fragment.getClass()
				.getName());
		transaction.commit();
	}
	public static void replaceTranslate(FragmentManager fragmentManager,
			int containerId, Fragment fragment, boolean addToBackStack) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (addToBackStack) {
			transaction.addToBackStack(null);
		}
		if (null == fragment) {
			return;
		}
		transaction.setCustomAnimations(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
		transaction.replace(containerId, fragment, fragment.getClass()
				.getName());
		transaction.commit();
	}

	public static void replaceZoom(FragmentManager fragmentManager,
			int containerId, Fragment fragment, boolean addToBackStack) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (addToBackStack) {
			transaction.addToBackStack(null);
		}
		// transaction.setCustomAnimations(android.R.animator.fade_in,
		// android.R.animator.fade_out);
		if (null == fragment) {
			return;
		}
		transaction.setCustomAnimations(android.R.anim.fade_in,
				android.R.anim.fade_out);
		transaction.replace(containerId, fragment, fragment.getClass()
				.getName());
		transaction.commit();
	}

	public static void replace(FragmentActivity activity, Fragment f,
			int container) {

		FragmentTransaction fragmentTransaction = activity
				.getSupportFragmentManager().beginTransaction();
		fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
				android.R.anim.fade_in, android.R.anim.fade_in,
				android.R.anim.fade_in);
		fragmentTransaction.replace(container, f);
		fragmentTransaction.commitAllowingStateLoss();
	}

	public static void add(
			FragmentManager fragmentManager, int containerId,
			Fragment addFragment) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.setCustomAnimations(android.R.anim.fade_in,
				android.R.anim.fade_out);
		if (null == addFragment) {
			return;
		}
		String tag = addFragment.getClass().getSimpleName();
		Fragment storedFragment = fragmentManager.findFragmentByTag(tag);
		hide(fragmentManager);
		if (storedFragment != null) {
			transaction.show(storedFragment);
		} else {
			try {
				transaction.add(containerId, addFragment, tag);
			} catch (Exception e) {
				Logging.print(e);
			}

		}

		transaction.commitAllowingStateLoss();
	}

	private static void hide(
			FragmentManager fragmentManager) {
		try {
			FragmentTransaction transaction2 = fragmentManager
					.beginTransaction();
			List<Fragment> fragments = fragmentManager.getFragments();
			if (fragments  != null) {
				for (Fragment fragment : fragments) {
					if (fragment != null) {
						transaction2.hide(fragment);
					}
				}
			}
			transaction2.commitAllowingStateLoss();
		} catch (Throwable e) {
			Logging.print(e);
		}
	}

	public static AlertDialog getDialog(Context context, final String title,
			final String message, final String negativeText,
			final String positiveText,
			final DialogInterface.OnClickListener clickListener) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		if (!TextUtils.isEmpty(title)) {
			alertDialog.setTitle(title);
		}
		if (!TextUtils.isEmpty(message)) {
			alertDialog.setMessage(message);
		}

		if (!TextUtils.isEmpty(positiveText)) {
			alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
					positiveText, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							if (clickListener != null) {
								clickListener.onClick(dialog, which);
							}

						}
					});
		}

		if (!TextUtils.isEmpty(negativeText)) {
			alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
					negativeText, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							if (clickListener != null) {
								clickListener.onClick(dialog, which);
							}

						}
					});
		}
		alertDialog.setCancelable(true);
		return alertDialog;
	}

	public static AlertDialog getDialogWithEditText(final Context context,
			final String title, final String message, final String edtString,
			final String negativeText, final String positiveText,
			final OnDialogEditextListener clickListener) {
		Builder builder = new AlertDialog.Builder(context);
		final AlertDialog alertDialog = builder.create();
		final EditText editText = new EditText(context);
		editText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		editText.setText(edtString);
		editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					alertDialog
							.getWindow()
							.setSoftInputMode(
									WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
				}
			}
		});
		// request keyboard

		alertDialog.setView(editText);
		if (!TextUtils.isEmpty(title)) {
			alertDialog.setTitle(title);
		}
		if (!TextUtils.isEmpty(message)) {
			alertDialog.setMessage(message);
		}

		if (!TextUtils.isEmpty(positiveText)) {
			alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
					positiveText, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							if (clickListener != null) {
								clickListener.onClick(dialog, which,
										editText.getEditableText());
							}

						}
					});
		}

		if (!TextUtils.isEmpty(negativeText)) {
			alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
					negativeText, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							if (clickListener != null) {
								clickListener.onClick(dialog, which,
										editText.getEditableText());
							}

						}
					});
		}

		alertDialog.setCancelable(true);
		return alertDialog;
	}

	public static interface OnDialogEditextListener {
		void onClick(DialogInterface dialog, int which, Editable editInput);
	}

}
